var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths:'../../img/id-face.png',
    tempFilePaths2: '../../img/id-back.png',
    tempFilePaths3: '../../img/d-face.png',
    tempFilePaths4: '../../img/d-back.png',
  },

  // 选择图片
  upimg: function () {
    var that = this;
    wx.chooseImage({
      // 设置最多可以选择的图片张数，默认9,如果我们设置了多张,那么接收时//就不在是单个变量了,
      count: 1,
      sizeType: ['original'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (response) {
        // 获取成功,将获取到的地址赋值给临时变量
        var tempFilePaths = response.tempFilePaths;
        that.setData({
          //将临时变量赋值给已经在data中定义好的变量
          tempFilePaths: tempFilePaths
        })
        wx.uploadFile({
          url: 'https://small.qiongyazuche.com/sunday/small/upload/uploadSingle',
          filePath: tempFilePaths[0],
          name: 'file',
          method: "POST",
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            //var data = res
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              fileData: res.data
            })
            let data = JSON.parse(res.data)
            //console.log("code" + data.code)
            console.log("身份证人像面" + data.result.halfPath)
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              idCardFront: data.result.halfPath
            })
          }
        })
      }
    })

  },

  // 选择图片
  upimg2: function () {
    var that = this;
    wx.chooseImage({
      // 设置最多可以选择的图片张数，默认9,如果我们设置了多张,那么接收时//就不在是单个变量了,
      count: 1,
      sizeType: ['original'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (response) {
        // 获取成功,将获取到的地址赋值给临时变量
        var tempFilePaths = response.tempFilePaths;
        that.setData({
          //将临时变量赋值给已经在data中定义好的变量
          tempFilePaths2: tempFilePaths
        })
        wx.uploadFile({
          url: 'https://small.qiongyazuche.com/sunday/small/upload/uploadSingle',
          filePath: tempFilePaths[0],
          name: 'file',
          method: "POST",
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            //var data = res
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              fileData: res.data
            })
            let data = JSON.parse(res.data)
            //console.log("code" + data.code)
            console.log("身份证国徽面" + data.result.halfPath)
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              idCardBack: data.result.halfPath
            })
          }
        })
      }
    })

  },
  // 选择图片
  upimg3: function () {
    var that = this;
    wx.chooseImage({
      // 设置最多可以选择的图片张数，默认9,如果我们设置了多张,那么接收时//就不在是单个变量了,
      count: 1,
      sizeType: ['original'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (response) {
        // 获取成功,将获取到的地址赋值给临时变量
        var tempFilePaths = response.tempFilePaths;
        that.setData({
          //将临时变量赋值给已经在data中定义好的变量
          tempFilePaths3: tempFilePaths
        })
        wx.uploadFile({
          url: 'https://small.qiongyazuche.com/sunday/small/upload/uploadSingle',
          filePath: tempFilePaths[0],
          name: 'file',
          method: "POST",
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            //var data = res
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              fileData: res.data
            })
            let data = JSON.parse(res.data)
            //console.log("code" + data.code)
            console.log("驾驶证主页" + data.result.halfPath)
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              driverFront: data.result.halfPath
            })
          }
        })
      }
    })

  },
  // 选择图片
  upimg4: function () {
    var that = this;
    wx.chooseImage({
      // 设置最多可以选择的图片张数，默认9,如果我们设置了多张,那么接收时//就不在是单个变量了,
      count: 1,
      sizeType: ['original'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (response) {
        // 获取成功,将获取到的地址赋值给临时变量
        var tempFilePaths = response.tempFilePaths;
        that.setData({
          //将临时变量赋值给已经在data中定义好的变量
          tempFilePaths4: tempFilePaths
        })
        wx.uploadFile({
          url: 'https://small.qiongyazuche.com/sunday/small/upload/uploadSingle',
          filePath: tempFilePaths[0],
          name: 'file',
          method: "POST",
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            //var data = res
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              fileData: res.data
            })
            let data = JSON.parse(res.data)
            //console.log("code" + data.code)
            console.log("驾驶证副业" + data.result.halfPath)
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              driverBack: data.result.halfPath
            })
          }
        })
      }
    })

  },

  // 提交成功弹窗
  showModal: function () {
    var idCardFront = this.data.idCardFront
    var idCardBack = this.data.idCardBack
    var driverFront = this.data.driverFront
    var driverBack = this.data.driverBack
    if (util.isBlank(idCardFront)){
      wx.showToast({
        title: '请上传身份证人像面',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (util.isBlank(idCardBack)) {
      wx.showToast({
        title: '请上传身份证国徽面',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (util.isBlank(driverFront)) {
      wx.showToast({
        title: '请上传驾驶证正面',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (util.isBlank(driverBack)) {
      wx.showToast({
        title: '请上传驾驶证副面',
        icon: 'none',
        duration: 1500
      })
      return
    }
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/member/updateMember',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'memberId': app.globalData.userInfo.id,
        'idCardFront': idCardFront,
        'idCardBack': idCardBack,
        'driverFront': driverFront,
        'driverBack': driverBack
      },
      success: function (res) {
        if (res.data.code == -1) {
          wx.showToast({
            title: '系统异常',
            icon: 'none',
            duration: 1500
          })
        } else {
          wx.showModal({
            showCancel: false,
            confirmColor: '#008cf8',
            title: '提交成功',
            content: '您的证件已提交 我们将在24小时内完成审核',
            success: function (res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '../index/index'
                })
              }
            }
          })
        }

      }
    })


    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '琼崖租车',
      path: '/pages/index/index'
    }
  },
  setUserInfo: function () {
    let that = this
    var data = { 'memberId': app.globalData.userInfo.id }
    var promise = app.synPostData('member/findOne', data)
    promise.then(function (res) {
      that.setData({
        userInfo: res.data.result
      })
      app.globalData.userInfo = res.data.result
      if (that.data.userInfo.userType == 3) {
        that.setData({
          isMember: false
        })
      } else {
        that.setData({
          isMember: true
        })
      }

    });
  }
}) 